package org.whmmm.util.httputil;

import org.whmmm.util.httputil.config.BaseRequest;
import org.whmmm.util.httputil.config.HttpHeader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <br/><br/>
 * 创建时间: 2020/5/14 11:15
 *
 * @author whmmm
 */
public class HttpRequest {

    protected List<HttpHeader> headList = new ArrayList<HttpHeader>();
    protected List<HttpHeader> cookieList = new ArrayList<HttpHeader>();
    protected Map<String, String> respHeader = new HashMap<String, String>();
    protected Map<String, String> respCookie = new HashMap<String, String>();

    protected String doRequest(String url, String method, String param) {
        return BaseRequest.CACHE_REQUEST.httpRequest(url, method,
                param, headList, cookieList,

                this.respHeader,
                this.respCookie);
    }

    public String get(String url) {
        return doRequest(url, "GET", "");
    }

    public String post(String url, String param) {
        return doRequest(url, "POST", param);
    }

    public HttpRequest withHeader(String key, String value) {
        this.headList.add(new HttpHeader(key, value));
        return this;
    }

    /**
     * 和 withHeader("Cookie","xxxxx") 效果一样!!
     *
     * @param key
     * @param value
     * @return
     */
    public HttpRequest withCookie(String key, String value) {
        this.cookieList.add(new HttpHeader(key, value));
        return this;
    }

    /**
     * 获取响应信息
     *
     * @return
     */
    public Map<String, String> getResponseHeader() {
        return respHeader;
    }

    /**
     * 获取 响应的 cookie 字符串
     *
     * @return
     */
    public String getRespCookie() {
        return respHeader.get("Set-Cookie");
    }

    /**
     * 获取 cookie => key,value 格式
     *
     * @return
     */
    public Map<String, String> getResponseCookie() {
        return respCookie;
    }



}
